package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCompany;
import org.jeecg.modules.job.rms.entity.RmsCompanyPosition;
import org.jeecg.modules.job.rms.mapper.RmsCompanyMapper;
import org.jeecg.modules.job.rms.mapper.RmsCompanyPositionMapper;
import org.jeecg.modules.job.ums.entity.UmsBrowse;
import org.jeecg.modules.job.ums.entity.UmsShareLog;
import org.jeecg.modules.job.ums.mapper.UmsBrowseMapper;
import org.jeecg.modules.job.ums.mapper.UmsShareLogMapper;
import org.jeecg.modules.job.ums.service.IUmsBrowseService;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.jeecg.modules.job.wms.entity.WmsJobExpect;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.jeecg.modules.job.wms.entity.WmsResumeFile;
import org.jeecg.modules.job.wms.entity.WmsWorkExp;
import org.jeecg.modules.job.wms.mapper.WmsJobExpectMapper;
import org.jeecg.modules.job.wms.mapper.WmsResumeMapper;
import org.jeecg.modules.job.wms.mapper.WmsWorkExpMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 浏览记录
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class UmsBrowseServiceImpl extends ServiceImpl<UmsBrowseMapper, UmsBrowse> implements IUmsBrowseService {

    @Resource
    private RmsCompanyPositionMapper companyPositionMapper;
    @Resource
    private RmsCompanyMapper companyMapper;
    @Resource
    private WmsResumeMapper resumeMapper;
    @Resource
    private WmsWorkExpMapper workExpMapper;
    @Resource
    private WmsJobExpectMapper jobExpectMapper;
    @Resource
    private UmsShareLogMapper shareLogMapper;
    @Resource
    private IUmsMemberRoleService memberRoleService;
    @Resource
    private ISysBaseAPI sysBaseAPI;

    @Override
    public boolean update(UmsBrowse param) {
        UmsBrowse result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            param.setId(result.getId());
            this.updateById(param);
        }else{
            this.save(param);
        }
        //携带推广码，增加推广查阅记录
        if (StringUtils.isNotEmpty(param.getTcode())){
            UmsShareLog shareLogResult=shareLogMapper.selectOne(new QueryWrapper<>(new UmsShareLog().setDataId(param.getPositionId()).setTcode(param.getTcode())));
            if (shareLogResult!=null){
                shareLogResult.setNumber(shareLogResult.getNumber()+1);
                shareLogMapper.updateById(shareLogResult);
            }
        }
        //添加浏览次数
        if (param.getBrowseType()==0){
            RmsCompanyPosition companyPosition =companyPositionMapper.selectById(param.getPositionId());
            companyPosition.setBrowseNumber(companyPosition.getBrowseNumber()+1);
            companyPositionMapper.updateById(companyPosition);
        }else{
            WmsResume resume=resumeMapper.selectById(param.getResumeId());
            resume.setBrowseNumber(resume.getBrowseNumber()+1);
            resumeMapper.updateById(resume);
        }
        return false;
    }

    @Override
    public IPage<UmsBrowse> getBrowsePageList(Page<UmsBrowse> page, UmsBrowse params) {
        return this.baseMapper.getBrowsePageList(page,params);
    }

    @Override
    public IPage<UmsBrowse> findPageBySpec(Page<UmsBrowse> page, UmsBrowse param) {
        IPage<UmsBrowse> pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        pageInfo.getRecords().forEach(item -> {
            if (item.getPositionId()!=null){
                item.setPosition(companyPositionMapper.selectOne(new QueryWrapper<>(new RmsCompanyPosition().setId(item.getPositionId()))));
                //招聘者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getComMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                //企业信息
                RmsCompany company=companyMapper.selectById(item.getCompanyId());
                item.setCompanyName(company.getName());
                item.setCompanyStaffSize(company.getStaffSize());
                item.setCompanyNature(company.getCompanyNature());
            }
            if (item.getResumeId()!=null){
                item.setResume(resumeMapper.selectOne(new QueryWrapper<>(new WmsResume().setId(item.getResumeId()))));
            }
        });
        return pageInfo;
    }

    @Override
    public IPage<UmsBrowse> findPageBySpec2(Page<UmsBrowse> page, UmsBrowse param) {
        IPage<UmsBrowse> pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        pageInfo.getRecords().forEach(item -> {
            if (item.getBrowseType()==0){//求职者看职位
                item.setResume(resumeMapper.selectOne(new QueryWrapper<>(new WmsResume().setMemberId(item.getMemberId()))));
                //求职者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getComMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                item.setMemberSex(memberResult.getSex()+"");
                //工作经历
                List<WmsWorkExp> list=workExpMapper.selectList(new QueryWrapper<>(new WmsWorkExp().setMemberId(item.getMemberId())));
                if (list.size()>0){
                    item.setCompanyName(list.get(0).getCompanyName());
                    item.setCompanyPostName(list.get(0).getPostName());
                    item.setWorkSkill(list.get(0).getSkill());
                }
                //期望薪资
                List<WmsJobExpect> expectResults =jobExpectMapper.selectList(new QueryWrapper<>(new WmsJobExpect().setId(item.getMemberId())));
                if (expectResults.size()>0){
                    item.setExpectSalary(expectResults.get(0).getExpectSalary());
                }
                //应聘岗位名称
                RmsCompanyPosition companyPositionResult =companyPositionMapper.selectOne(new QueryWrapper<>(new RmsCompanyPosition().setId(item.getPositionId())));
                if (companyPositionResult!=null){
                    item.setPostName(companyPositionResult.getPostName());
                }
            }else if (item.getBrowseType()==1){//招聘者看简历
                item.setCompany(companyMapper.selectOne(new QueryWrapper<>(new RmsCompany().setId(item.getCompanyId()))));
            }
        });
        return pageInfo;
    }
}
